package com.lightjet.macross.base.upms.controller;

import com.lightjet.macross.base.feign.api.domain.AuthUser;
import com.lightjet.macross.base.upms.domain.RoleInfo;
import com.lightjet.macross.base.upms.domain.RoleUserLink;
import com.lightjet.macross.base.upms.service.LoginAdapterService;
import com.lightjet.macross.base.upms.service.RoleInfoService;
import com.lightjet.macross.base.upms.service.RoleUserLinkService;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RespUtil;
import com.lightjet.macross.common.util.SpringUtil;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/roleuser")
@Scope("request")
@Log
@Api(tags="通用系统权限管理-角色用户相关接口集")
public class RoleUserController {

    @Value("${biz.usersystembeanname:macross}")
    private String usersystembeanname;

    @Resource
    private RoleUserLinkService roleUserLinkService;

    @Resource
    private RoleInfoService roleInfoService;

    @GetMapping("/list")
    @ApiOperation(value = "系统角色用户查询接口", notes = "根据角色ID获取该角色下的所有用户集合")
    public ResponseDTO<List<AuthUser>> GetByRoleId(@ApiParam(name="roleid",value = "角色ID") Integer roleid)
    {
        RoleInfo roleInfo = roleInfoService.FindDistinctById(roleid);
        String userbean = (roleInfo.getBelongsystem()!=null && !StringUtil.isNullOrEmpty(roleInfo.getBelongsystem().getUsibn()))?roleInfo.getBelongsystem().getUsibn():usersystembeanname;
        LoginAdapterService loginAdapterService = SpringUtil.getBean(userbean,LoginAdapterService.class);
        return RespUtil.CreateOK(roleUserLinkService.FindByRoleinfo(new RoleInfo(roleid)).stream().map(e-> loginAdapterService.GetByUserKey(e.getUserkey())).collect(Collectors.toList()));
    }

    @PostMapping(value = "/grant",consumes = "application/json")
    @ApiOperation(value = "授予用户角色接口", notes = "根据人员ID集合Json分配到给定的角色ID")
    public ResponseDTO<String> Grant(
            @ApiParam(name="users",value = "用户List Json") @RequestBody List<String> users,
            @ApiParam(name="roleid",value = "角色ID") Integer roleid)
    {
        return roleUserLinkService.GrantRoleToUsers(new RoleInfo(roleid),users);
    }

    @PostMapping(value = "/revoke",consumes = "application/json")
    @ApiOperation(value = "驳回用户角色接口", notes = "根据人员ID集合Json从给定的角色ID撤销用户")
    public ResponseDTO<String> Revoke(
            @ApiParam(name="users",value = "用户List Json") @RequestBody List<String> users,
            @ApiParam(name="roleid",value = "角色ID") Integer roleid)
    {
        return roleUserLinkService.RevokeRoleToUsers(new RoleInfo(roleid),users);
    }

}
